import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import GoodsList from '../components/goods/List.vue'
import Add from '../components/goods/Add.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'


Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home', component: Home, redirect: '/welcome',
    children: [
      // 子路由Welcom
      { path: '/welcome', component: Welcome },
      // 子路由Users
      { path: '/users', component: Users },
      // 子路由Rights
      { path: '/rights', component: Rights },
      // 子路由Roles
      { path: '/roles', component: Roles },
      // 子路由Cate
      { path: '/categories', component: Cate },
      // 子路由Params
      { path: '/params', component: Params },
      // 子路由GoodsList
      { path: '/goods', component: GoodsList },
      // 子路由add
      { path: '/goods/add', component: Add },
      // 子路由Order
      { path: '/orders', component: Order },
      // 子路由Report
      { path: '/reports', component: Report },
    ]
  }
]



const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to代表将要访问的路径 from表示你是从哪个路径跳转来的 next是一个放行函数 next() 直接放行 next('/login') 强制跳转

  // 如果访问的是登录页就直接放行
  if (to.path === '/login') return next()

  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')

  // 如果token是空的 就直接让他去登录
  if (!tokenStr) return next('/login')

  // 上面的两种情况都没有发生 就放行
  next()
})

export default router
